package product;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

public class DeliveryUpdate extends JInternalFrame {

	Map<Long, DeliveryData> delivery = new HashMap<>();
	Map<String, CompanyData> customer = new HashMap<>();
	DeliveryData dd = null;
	DefaultTableModel model2;

	ProductMain4 main;

	boolean flag = false;
	long index;
	long oriTotal;
	SimpleDateFormat sdf;
	DecimalFormat df = new DecimalFormat("###,###,###,###,### 원");

	private JLabel lblNewLabel;
	JTextField inputCode;
	private JLabel lblNewLabel_1;
	JTextField inputName;
	private JLabel lblNewLabel_2;
	JTextField inputCEO;
	private JLabel lblNewLabel_3;
	JTextField inputItemName;
	private JLabel lblLogo;
	private JButton btnCodeSearch;
	private JButton btnDelete;
	private JButton btnUpdate;
	private JLabel lblNewLabel_5;
	private JLabel lblNewLabel_6;
	JTextField inputPrice;
	JTextField inputTotal;
	private JButton btnNewButton;

	private JButton btnNewButton_1;

	JSpinner inputEa;
	JTable table;

	int row;
	JTextField inputDeliverynum;
	private JLabel xxx;
	private JPanel panel;
	private JPanel panel_1;
	private JLabel lblQuantity;
	private JLabel lblNewLabel_4;
	private JLabel lblDeliveryInfo;
	private JLabel lblNewLabel_8;
	private JButton btnNewButton_3;
	private JButton btnNewButton_4;
	private JTextField textField;
	private JPanel panel_3;
	private JButton btnNewButton_2;

	public DeliveryUpdate(ProductMain4 main) {
		super("Delivery Update", false, true, false, true);
		getContentPane().setBackground(Color.WHITE);
		setBounds(0, 0, 480, 453);
		getContentPane().setLayout(null);
		getContentPane().add(getPanel());
		getContentPane().add(getPanel_1());
		getContentPane().add(getLblNewLabel_4());
		getContentPane().add(getLblDeliveryInfo());
		getContentPane().add(getBtnNewButton_3());
		getContentPane().add(getBtnNewButton_4());
		getContentPane().add(getBtnNewButton_2());
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.main = main;
		setDate();
	}

	public int replaceNum(String s){
		int num = Integer.parseInt(s.replace("," , "").replace("원", "").trim());
		return num;
	}
	
	public void setDate() {// 등록일
		sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String a = sdf.format((new Date()));

	}

	public void table0(JTable t, DefaultTableModel model2, int row) {
		this.table = t;
		this.model2 = model2;
		this.row = row;

		
	}

	public void searchDB() {

		String sql = "select a.*, b.pro_price as 원가, (a.수량*b.pro_price) as 지불금액 , b.pro_nm as 제품명 "
				+ "from(select distinct d.num as 납품번호, d.customer_num as 사업자번호, c.name as 회사명, "
				+ "c.ceo as 대표자명, d.ea as 수량, d.code as 제품코드 from delivery d inner join customer c "
				+ "on d.customer_num=c.num ) a inner join code b on a.제품코드 = b.pro_code and a.납품번호 = ?";

		try {
			// OracleJdbc o = new OracleJdbc();
			// main.conn = o.conn;

			PreparedStatement pst = main.conn.prepareStatement(sql);
			pst.setInt(1, Integer.parseInt((inputDeliverynum.getText())));
			if (pst.executeUpdate() > 0) {
				ResultSet rs = pst.executeQuery();
				while (rs.next()) {
					inputCode.setText(rs.getString(2)); // 사업자번호
					inputName.setText(rs.getString(3)); // 회사명
					inputCEO.setText(rs.getString(4)); // 대표자
					
					inputEa.setValue(rs.getInt(5)); // 수량
					inputPrice.setText(df.format(rs.getInt(7))); // 원가
					inputTotal.setText(df.format(rs.getInt(8))); // 지불금액
					inputItemName.setText(rs.getString(9)); // 제품명
					
					lblLogo.setIcon(new ImageIcon("img\\logo\\" + inputCode.getText()+ ".png"));
				}
				rs.close();
			} else {
				System.out.println("searchDB 실패");
			}

			pst.close();
			// main.conn.close();

		} catch (Exception ex) {
			ex.getMessage();
			System.out.println("searchDB 에러");
		}

	}

	public void compute() {
		try {
			if (!inputPrice.getText().equals("")) {
				oriTotal = replaceNum(inputPrice.getText()) * Long.parseLong(inputEa.getValue().toString());
				inputTotal.setText(df.format(oriTotal));
			}
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "단가와 수량에는 숫자만 입력하세요.");
		}
	}

	public void reSet() {
		inputDeliverynum.setText("");
		inputCode.setText("");
		inputName.setText("");
		inputCEO.setText("");
		inputItemName.setText("");
		inputEa.setValue(0);
		inputPrice.setText("");
		inputTotal.setText("");
		lblLogo.setIcon(new ImageIcon());

		setVisible(false);
	}

	public void dbModi() {
		try {
			// OracleJdbc o = new OracleJdbc();
			// main.conn = o.conn;

			int a = Integer.parseInt(inputDeliverynum.getText());
			int b = Integer.parseInt(inputEa.getValue().toString());
			String sql = "update Delivery set ea=? where num= ?";
			PreparedStatement pst = main.conn.prepareStatement(sql);
			pst.setInt(1, b);
			pst.setInt(2, a);

			pst.executeUpdate();
			pst.close();
			// main.conn.close();

		} catch (Exception ex) {
			ex.getMessage();

		}
	}

	public void dbDele() {
		try {
			// OracleJdbc o = new OracleJdbc();
			// main.conn = o.conn;

			int a = Integer.parseInt(inputDeliverynum.getText());
			String sql = "Delete from  Delivery where num= ?";
			PreparedStatement pst = main.conn.prepareStatement(sql);
			pst.setInt(1, a);

			pst.executeUpdate();
			pst.close();
			// main.conn.close();

		} catch (Exception ex) {
			ex.getMessage();

		}

	}

	private JButton getBtnDelete() {
		if (btnDelete == null) {
			btnDelete = new JButton("삭제");
			btnDelete.setBounds(12, 80, 92, 25);
			btnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					int yn = JOptionPane.showConfirmDialog(null,
							"정말 삭제 하시겠습니까??");
					if (yn == JOptionPane.OK_OPTION) {
						JOptionPane.showMessageDialog(null,
								"삭제되었습니다. 돌이킬 수 없습니다.");

						dbDele();
						reSet();

					}
				}
			});
		}
		return btnDelete;
	}

	private JButton getBtnUpdate() {
		if (btnUpdate == null) {
			btnUpdate = new JButton("수정");
			btnUpdate.setBounds(12, 45, 92, 25);
			btnUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String a=inputEa.getValue().toString();
					if(inputDeliverynum.getText().equals("")){
						JOptionPane.showMessageDialog(null, "데이터가 선택되지 않았습니다. 다시 선택해주세요.");
					}else if(main.tList.getTable().getValueAt(0, 2).equals(a)){
						
						JOptionPane.showMessageDialog(null, "변경된 수량이 없습니다.");
					}else{
						int yn = JOptionPane.showConfirmDialog(null,
								"수정된 정보를 저장 하시겠습니까??");
						if (yn == JOptionPane.OK_OPTION) {
							JOptionPane.showMessageDialog(null, "자~~알 저장되었습니다.");
	
							dbModi();
							reSet();
						}
					}
				}
			});
		}
		return btnUpdate;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Registration Number ");
			lblNewLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
			lblNewLabel.setBounds(150, 11, 170, 25);
			lblNewLabel.setPreferredSize(new Dimension(70, 20));
		}
		return lblNewLabel;
	}

	private JTextField getInputCode() {
		if (inputCode == null) {
			inputCode = new JTextField();
			inputCode.setHorizontalAlignment(SwingConstants.CENTER);
			inputCode.setBackground(SystemColor.menu);
			inputCode.setBorder(new LineBorder(Color.GRAY));
			inputCode.setFont(new Font("돋움", Font.PLAIN, 11));
			inputCode.setBounds(302, 11, 124, 25);
			inputCode.setEditable(false);
			inputCode.setPreferredSize(new Dimension(150, 21));
			inputCode.setColumns(10);
		}
		return inputCode;
	}

	/**
	 * @return
	 * @uml.property name="lblNewLabel_1"
	 */
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ChairMan");
			lblNewLabel_1.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
			lblNewLabel_1.setBounds(150, 83, 170, 25);
		}
		return lblNewLabel_1;
	}

	/**
	 * @return
	 * @uml.property name="inputName"
	 */
	private JTextField getInputName() {
		if (inputName == null) {
			inputName = new JTextField();
			inputName.setHorizontalAlignment(SwingConstants.CENTER);
			inputName.setBackground(SystemColor.menu);
			inputName.setBorder(new LineBorder(Color.GRAY));
			inputName.setFont(new Font("돋움", Font.PLAIN, 11));
			inputName.setBounds(302, 47, 124, 25);
			inputName.setEditable(false);
			inputName.setColumns(10);
		}
		return inputName;
	}

	/**
	 * @return
	 * @uml.property name="lblNewLabel_2"
	 */
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("Legal Business Name");
			lblNewLabel_2.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
			lblNewLabel_2.setBounds(150, 47, 170, 25);
		}
		return lblNewLabel_2;
	}

	/**
	 * @return
	 * @uml.property name="inputCEO"
	 */
	private JTextField getInputCEO() {
		if (inputCEO == null) {
			inputCEO = new JTextField();
			inputCEO.setHorizontalAlignment(SwingConstants.CENTER);
			inputCEO.setBackground(SystemColor.menu);
			inputCEO.setBorder(new LineBorder(Color.GRAY));
			inputCEO.setFont(new Font("돋움", Font.PLAIN, 11));
			inputCEO.setBounds(302, 83, 124, 25);
			inputCEO.setEditable(false);
			inputCEO.setColumns(10);
		}
		return inputCEO;
	}

	/**
	 * @return
	 * @uml.property name="lblNewLabel_3"
	 */
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("Product Name");
			lblNewLabel_3.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
			lblNewLabel_3.setBounds(26, 45, 170, 25);
		}
		return lblNewLabel_3;
	}

	/**
	 * @return
	 * @uml.property name="inputItemName"
	 */
	private JTextField getInputItemName() {
		if (inputItemName == null) {
			inputItemName = new JTextField();
			inputItemName.setFont(new Font("돋움", Font.PLAIN, 11));
			inputItemName.setBorder(new LineBorder(Color.GRAY));
			inputItemName.setEditable(false);
			inputItemName.setBounds(160, 45, 117, 25);
			inputItemName.setColumns(10);
		}
		return inputItemName;
	}

	/**
	 * @return
	 * @uml.property name="lblLogo"
	 */
	private JLabel getLblLogo() {
		if (lblLogo == null) {
			lblLogo = new JLabel("");
			lblLogo.setBackground(SystemColor.info);
			lblLogo.setBounds(12, 12, 100, 100);
			lblLogo.setHorizontalAlignment(SwingConstants.CENTER);
			lblLogo.setBorder(new LineBorder(Color.GRAY, 2));
		}
		return lblLogo;
	}

	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("Unit Price");
			lblNewLabel_5.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
			lblNewLabel_5.setBounds(26, 115, 170, 25);
		}
		return lblNewLabel_5;
	}

	/**
	 * @return
	 * @uml.property name="lblNewLabel_6"
	 */
	private JLabel getLblNewLabel_6() {
		if (lblNewLabel_6 == null) {
			lblNewLabel_6 = new JLabel("Total Amount");
			lblNewLabel_6.setFont(new Font("Microsoft YaHei", Font.BOLD, 12));
			lblNewLabel_6.setBounds(26, 150, 170, 25);
		}
		return lblNewLabel_6;
	}

	/**
	 * @return
	 * @uml.property name="inputPrice"
	 */
	private JTextField getInputPrice() {
		if (inputPrice == null) {
			inputPrice = new JTextField();
			inputPrice.setHorizontalAlignment(SwingConstants.RIGHT);
			inputPrice.setFont(new Font("돋움", Font.PLAIN, 11));
			inputPrice.setBorder(new LineBorder(Color.GRAY));
			inputPrice.setEditable(false);
			inputPrice.setBounds(160, 115, 117, 25);
			inputPrice.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					compute();
				}
			});
			inputPrice.setColumns(10);
		}
		return inputPrice;
	}

	/**
	 * @return
	 * @uml.property name="inputTotal"
	 */
	private JTextField getInputTotal() {
		if (inputTotal == null) {
			inputTotal = new JTextField();
			inputTotal.setHorizontalAlignment(SwingConstants.RIGHT);
			inputTotal.setFont(new Font("돋움", Font.PLAIN, 11));
			inputTotal.setBackground(SystemColor.info);
			inputTotal.setBorder(new LineBorder(Color.GRAY));
			inputTotal.setBounds(160, 150, 117, 25);
			inputTotal.setEditable(false);
			inputTotal.setColumns(10);
		}
		return inputTotal;
	}

	/**
	 * @return
	 * @uml.property name="inputEa"
	 */
	private JSpinner getInputEa() {
		if (inputEa == null) {
			inputEa = new JSpinner();
			//inputEa.setModel(new SpinnerListModel(new String[] {}));
			inputEa.setFont(new Font("돋움", Font.PLAIN, 11));
			inputEa.setBorder(new LineBorder(Color.GRAY));
			inputEa.setBounds(160, 80, 117, 25);
			inputEa.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent arg0) {
					compute();
				}
			});
		}
		return inputEa;
	}

	public JTextField getInputDeliverynum() {
		if (inputDeliverynum == null) {
			inputDeliverynum = new JTextField();
			inputDeliverynum.setFont(new Font("돋움", Font.PLAIN, 11));
			inputDeliverynum.setBorder(new LineBorder(Color.GRAY));
			inputDeliverynum.setEditable(false);
			inputDeliverynum.setBounds(160, 10, 117, 25);
			inputDeliverynum.setColumns(10);
		}
		return inputDeliverynum;
	}

	private JLabel getXxx() {
		if (xxx == null) {
			xxx = new JLabel("Delivery Number");
			xxx.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
			xxx.setBounds(26, 10, 170, 25);
		}
		return xxx;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.inactiveCaptionBorder);
			panel.setBorder(new LineBorder(Color.GRAY, 2));
			panel.setBounds(10, 47, 446, 122);
			panel.setLayout(null);
			panel.add(getLblNewLabel());
			panel.add(getInputCode());
			panel.add(getLblNewLabel_2());
			panel.add(getInputCEO());
			panel.add(getLblNewLabel_1());
			panel.add(getInputName());
			panel.add(getLblLogo());
			panel.add(getLblNewLabel_8());
		}
		return panel;
	}

	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBackground(SystemColor.inactiveCaptionBorder);
			panel_1.setBorder(new LineBorder(Color.GRAY, 2));
			panel_1.setBounds(10, 216, 446, 188);
			panel_1.setLayout(null);
			panel_1.add(getLblNewLabel_3());
			panel_1.add(getInputItemName());
			panel_1.add(getXxx());
			panel_1.add(getInputDeliverynum());
			panel_1.add(getInputEa());
			panel_1.add(getInputPrice());
			panel_1.add(getInputTotal());
			panel_1.add(getLblNewLabel_5());
			panel_1.add(getLblNewLabel_6());
			panel_1.add(getLblQuantity());
			panel_1.add(getPanel_3());
		}
		return panel_1;
	}

	private JLabel getLblQuantity() {
		if (lblQuantity == null) {
			lblQuantity = new JLabel("Quantity");
			lblQuantity.setFont(new Font("Microsoft YaHei", Font.PLAIN, 12));
			lblQuantity.setBounds(26, 80, 170, 25);
		}
		return lblQuantity;
	}

	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("Customer Information");
			lblNewLabel_4.setForeground(Color.DARK_GRAY);
			lblNewLabel_4.setFont(new Font("Microsoft YaHei", Font.PLAIN, 17));
			lblNewLabel_4.setBounds(10, 18, 192, 30);
		}
		return lblNewLabel_4;
	}

	private JLabel getLblDeliveryInfo() {
		if (lblDeliveryInfo == null) {
			lblDeliveryInfo = new JLabel("Delivery Information");
			lblDeliveryInfo.setForeground(Color.DARK_GRAY);
			lblDeliveryInfo
					.setFont(new Font("Microsoft YaHei", Font.PLAIN, 17));
			lblDeliveryInfo.setBounds(10, 187, 184, 30);
		}
		return lblDeliveryInfo;
	}

	private JLabel getLblNewLabel_8() {
		if (lblNewLabel_8 == null) {
			lblNewLabel_8 = new JLabel("New label");
			lblNewLabel_8.setBackground(Color.GRAY);
			lblNewLabel_8.setBorder(new LineBorder(Color.GRAY, 2));
			lblNewLabel_8.setBounds(128, 12, 2, 100);
		}
		return lblNewLabel_8;
	}

	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("");
			btnNewButton_3.setIcon(new ImageIcon("img\\add.png"));
			btnNewButton_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					main.openDeliveryInput();
				}
			});
			btnNewButton_3.setBounds(422, 185, 30, 30);
		}
		return btnNewButton_3;
	}

	private JButton getBtnNewButton_4() {
		if (btnNewButton_4 == null) {
			btnNewButton_4 = new JButton("");
			btnNewButton_4.setIcon(new ImageIcon("img\\mody.png"));
			btnNewButton_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					main.openCompanyUpdate();
					main.cpUpdate.getTxtNum().setText(inputCode.getText());
					main.cpUpdate.search();
					
				}
			});
			btnNewButton_4.setBounds(422, 16, 30, 30);
		}
		return btnNewButton_4;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(12, 10, 92, 25);
			textField.setFont(new Font("돋움", Font.PLAIN, 12));
			textField.setHorizontalAlignment(SwingConstants.CENTER);
			textField.setBackground(new Color(255, 192, 203));
			textField.setBorder(new LineBorder(Color.GRAY));
			textField.setText("재고수량");
			textField.setColumns(10);
		}
		return textField;
	}

	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setBackground(SystemColor.info);
			panel_3.setBounds(305, 45, 111, 115);
			panel_3.setLayout(null);
			panel_3.add(getTextField());
			panel_3.add(getBtnUpdate());
			panel_3.add(getBtnDelete());
		}
		return panel_3;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					main.openCompanyInput();
				}
			});
			btnNewButton_2.setIcon(new ImageIcon("img\\add.png"));
			btnNewButton_2.setBounds(388, 16, 30, 30);
		}
		return btnNewButton_2;
	}
}
